################################################################################
##
## Purpose: This script creates SI Figure 13
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/finalData.RData
##
## Output Files:
##  - ./output/figures/SI_figure_13.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(ggrepel)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

load('./data/prepped/finalData.RData')

slopeFn <- function(textGrepl,dropDims = 'AUTHOR|comb|LIKELY|SEVERE|UNSUBSTANTIAL|INCOHERENT',groupDim = 'ATTACK',wdth = 20,ords = NULL) {
  toplot <- utterance_level %>%
    filter(grepl(textGrepl,textclean)) %>%
    select(text = textclean,matches('SENT')) %>%
    group_by(text) %>%
    mutate(text = gsub('NA.*','',paste(stringi::stri_wrap(text,width = wdth)[1:min(length(.),10)],collapse = '\n'))) %>%
    select(-matches('lag$|error')) %>%
    gather(dim,sent,-text) %>%
    filter(!grepl(dropDims,dim)) %>%
    mutate(dim = ifelse(grepl(groupDim,dim),groupDim,dim)) %>%
    group_by(text,dim) %>%
    summarise(sent = mean(sent,na.rm=T)) %>%
    ungroup() %>%
    mutate(dim = gsub('_',' ',gsub('SENT_','',dim)))
  
  if(is.null(ords)) { 
    ords <- 1:length(unique(toplot$text))
  }
  
  levs <- levels(factor(unique(toplot$text)))[ords]
  
  toplot <- toplot %>%
    mutate(text = factor(text,levels = levs))
  
  toplot %>%
    ggplot(aes(x = text,y = sent,color = dim,group = dim,label = dim)) + 
    geom_line(size = 1.3) + 
    geom_text_repel(data = toplot %>% filter(text == levs[1]),hjust = 1,nudge_x = -.05,direction = 'both') + 
    geom_text_repel(data = toplot %>% filter(text == levs[length(levs)]),hjust = 0,nudge_x = .05,direction = 'both') + 
    geom_point(size = 5) + 
    scale_y_continuous(limits = c(0,1),name = 'Score (Pr label)',labels = scales::percent)
  
}

pdf('./output/figures/SI_figure_13.pdf',width = 9,height= 8)
slopeFn(textGrepl = "But, Chair Yellen, forgive me for the interruption.|Okay. I will take that. We have seen|invitation, they would love to have you in Hot Springs|Yet, seniors haven't seen a",
        dropDims = 'comb|OBSCENE|INCOHERENT|LIKELY|UNSUBSTANTIAL|SEXUALLY|SEVERE|COMMENTER|THREAT|TOXICITY|INSULT|INFLAMMATORY',
        groupDim = 'none',
        ords = c(1,4,2,3),
        wdth = 70) + 
  labs(title = 'Tone Results',
       subtitle = 'High-Scoring Responses to Yellen',
       x = NULL) + 
  theme_ridges() + 
  theme(axis.text.y = element_text(size = 10,vjust = .5),
        legend.position = 'none') + 
  coord_flip() + 
  scale_colour_brewer(name = 'Dimension',palette = "Dark2")
dev.off()

# EOF